package com.eascs.p2p.admin.manager.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.admin.manager.IAdminResourceManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.AdminResourceEntity;
import com.eascs.p2p.common.entity.AdminRoleEntity;





@Repository
public class AdminResourceManagerImpl extends BaseDao implements IAdminResourceManager {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public List<AdminResourceEntity> selectResourcesByRoles(List<AdminRoleEntity> roles) {
		
		if(roles==null || roles.size()==0){
			logger.info("error,method:必要参数不能为空!");
			return null;
		}
    	List<String> rids=new ArrayList<String>();
    	for (AdminRoleEntity sysRole : roles) {
    		rids.add(sysRole.getId());
		}
    	String str="('" + StringUtils.join(rids,"','") + "')";
    	StringBuffer sql=new StringBuffer();
    	sql.append(" SELECT res.id,res.resource_name,res.resource_code,res.resource_type,res.resDefault,res.resource_url,res.description,res.parent_id,res.sort,res.creator,res.updator,res.create_time,res.update_time");
    	sql.append(" FROM p2p_admin_role AS role");
    	sql.append(" LEFT JOIN p2p_admin_role_resources AS rm ON role.id = rm.role_id");
    	sql.append(" LEFT JOIN p2p_admin_resources AS res ON rm.resources_id = res.id");
    	sql.append(" WHERE role.id IN " + str +" AND res.resDefault='N' GROUP BY rm.resources_id");
        List<AdminResourceEntity> list = this.findEntitys(sql.toString(), AdminResourceEntity.class,new Object[]{});
        return list;
	}

	@Override
	public List<AdminResourceEntity> selectResourcesList() {
		
		StringBuffer sql=new StringBuffer();
    	sql.append(" SELECT res.id,res.resource_name,res.resource_code,res.resource_type,res.resDefault,res.resource_url,res.description,res.parent_id,res.sort,res.creator,res.updator,res.create_time,res.update_time");
    	sql.append(" FROM p2p_admin_resources AS res");
    	sql.append(" ORDER BY res.parent_id ASC,res.sort ASC");
        List<AdminResourceEntity> list = this.findEntitys(sql.toString(), AdminResourceEntity.class, new Object[] {});
        return list;
	}

	@Override
	public List<AdminResourceEntity> selectDefaultResourcesList() {
		StringBuffer sql=new StringBuffer();
    	sql.append(" SELECT res.id,res.resource_name,res.resource_code,res.resource_type,res.resDefault,res.resource_url,res.description,res.parent_id,res.sort,res.creator,res.updator,res.create_time,res.update_time");
    	sql.append(" FROM p2p_admin_resources AS res WHERE res.resDefault='Y'");
        List<AdminResourceEntity> list = this.findEntitys(sql.toString(), AdminResourceEntity.class, new Object[] {});
        return list;
	}

	@Override
	public List<AdminResourceEntity> selectResourcesListByCode(String code) {
		StringBuffer sql=new StringBuffer();
    	sql.append(" SELECT res.id,res.resource_name,res.resource_code,res.resource_type,res.resDefault,res.resource_url,res.description,res.parent_id,res.sort,res.creator,res.updator,res.create_time,res.update_time");
    	sql.append(" FROM p2p_admin_resources AS res");
    	sql.append(" WHERE res.resource_code=?");
    	 List<AdminResourceEntity> list = this.findEntitys(sql.toString(), AdminResourceEntity.class, new Object[] {code});
         return list;
	}

	@Override
	public List<AdminResourceEntity> selectUnDefaultResourcesList() {
		StringBuffer sql=new StringBuffer();
    	sql.append(" SELECT res.id,res.resource_name,res.resource_code,res.resource_type,res.resDefault,res.resource_url,res.description,res.parent_id,res.sort,res.creator,res.updator,res.create_time,res.update_time");
    	sql.append(" FROM p2p_admin_resources AS res WHERE res.resDefault='N'");
        List<AdminResourceEntity> list = this.findEntitys(sql.toString(), AdminResourceEntity.class, new Object[] {});
        return list;
	}

	@Override
	public String insertResources(AdminResourceEntity sysResources) {
		Date date = new Date();
		sysResources.setCreateTime(date);
		sysResources.setUpdateTime(date);

		boolean flag = this.insert(sysResources);
		if (flag == true) {
			return sysResources.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateResources(AdminResourceEntity sysResources) {
		StringBuffer sql=new StringBuffer();
    	sql.append(" update p2p_admin_resources set resource_url=?,creator=?,update_time=?,resource_code=?,parent_id=?,resource_type=?,resDefault=?,description=?,updator=?,sort=?,resource_name=? where id=?");
    	return this.execute(sql.toString(), new Object[]{sysResources.getResourceUrl(),sysResources.getCreator(),sysResources.getUpdateTime(),sysResources.getResourceCode(),sysResources.getParentId(),sysResources.getResourceType(),sysResources.getResourceDefault(),sysResources.getDescription(),sysResources.getUpdator(),sysResources.getSort(),sysResources.getResourceName(),sysResources.getId()});
	}

	@Override
	public int deleteResources(AdminResourceEntity sysResources) {
		return this.deleteByIdWithEntity(sysResources);
	}

	








}
